// 核心代码

// 引入模块
const express = require("express");
const socketio = require("socket.io");
const Socket = require("./core/socket");
const Game = require("./core/game");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackConfig = require("../../webpack.dev");

const app = express();
app.use(express.static("public"));

if (process.env.NODE_ENV === "development") {
  //开发模式，代码改动就使用webpack
  const compiler = webpack(webpackConfig);
  app.use(webpackDevMiddleware(compiler));
} else {
  app.use(express.static("dist"));
}
// 启动服务
const port = process.env.PROT || 3000;
const server = app.listen(port, () => {
  console.log(`server Listening on port:${port}`);
});

// 实例游戏类
const game = new Game();

// 监听socket服务
const io = socketio(server);

// 将游戏以及io传入创建的socket类统一管理
const socket = new Socket(game, io);

// 监听连接进游戏的回调
io.on("connect", (item) => {
  socket.listen(item);
});
